<?php
/**
 * Hols class Image_Lib_Mozaic
 *
 * @author daantimmer
 *
 * @package Library
 * @subpackage Image
 */

/**
 * Class that accepts an image and converts it to a moazic (basicly reduing the width and height by $blocksize
 *
 * @todo cleanup class, it used to set a block in a new image but now it only sets a new pixel in an image equal to oldimage/blocksize
 *
 * @author daantimmer
 */
class Image_Lib_Mozaic{
	
	/**
	 * See class description
	 *
	 * @param Image_Image $img				input image
	 * @param Image_GetBlockFactory $get	callback function class
	 * @param integer $blocksize			divisor/block size
	 * @return Image_Image
	 */
	public function makeMozaic(Image_Image $img, Image_GetBlockFactory $get, $blocksize){
		$newImg = new Image_Image($img->getWidth()/$blocksize, $img->getHeight()/$blocksize);
		
		for($y=0,$yn = $img->getHeight(); $y < $yn; $y+= $blocksize){
			for($x=0,$xn = $img->getWidth(); $x < $xn; $x+= $blocksize){
				$this->setBlock(
					$x,$y,$blocksize,
					$get->get($x,$y,$blocksize,$img),
					$newImg
				);
			}
		}
		
		return $newImg;
	}
	
	/**
	 * Sets a pixel with $color at ($xs,$ys) in image $img
	 *
	 * @todo cleanup this function
	 *
	 * @param integer $xs
	 * @param integer $ys
	 * @param integer $blocksize
	 * @param Image_Lib_Color $color
	 * @param Image_Image $img
	 * @return void
	 */
	protected function setBlock($xs,$ys,$blocksize, Image_Lib_Color $color,Image_Image $img){
//		$mw = min($xs+$blocksize, $img->getWidth());
//		$mh = min($ys+$blocksize, $img->getHeight());
//
//		for($y=$ys,$yn=$mh;$y<$yn;$y++){
//			for($x=$xs,$xn=$mw;$x<$xn;$x++){
//				$img->setColor($x,$y,$color);
//			}
//		}
		$img->setColor($xs/$blocksize,$ys/$blocksize,$color);
	}
}